import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.xssf.streaming.*;

import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;

import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.model.ThemesTable;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorderPr;

import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;

import java.io.FileOutputStream;
import java.io.IOException;

//对角线
class CellDiagonalBorders {

    private static CTBorder getCTBorder(StylesTable stylesSource, CTXf cellXf) {
        CTBorder ct;
        if (cellXf.getApplyBorder()) {
            int idx = (int) cellXf.getBorderId();
            XSSFCellBorder cf = stylesSource.getBorderAt(idx);
            ct = (CTBorder) cf.getCTBorder().copy();
        } else {
            ct = CTBorder.Factory.newInstance();
        }
        return ct;
    }

    public static void setBorderDiagonal(BorderStyle border, StylesTable stylesSource, CTXf cellXf, ThemesTable theme) {
        CTBorder ct = getCTBorder(stylesSource, cellXf);
        CTBorderPr pr = ct.isSetDiagonal() ? ct.getDiagonal() : ct.addNewDiagonal();
        if (border == BorderStyle.NONE) {
            ct.unsetDiagonal();
        } else {
            ct.setDiagonalDown(true);
            ct.setDiagonalUp(true);
            pr.setStyle(STBorderStyle.Enum.forInt(border.getCode() + 1));
        }
        int idx = stylesSource.putBorder(new XSSFCellBorder(ct, theme, stylesSource.getIndexedColors()));
        cellXf.setBorderId(idx);
        cellXf.setApplyBorder(true);
    }

    public static void main(String[] args) throws Exception {
        //Workbook wb = new XSSFWorkbook();
        Workbook wb = new SXSSFWorkbook(SXSSFWorkbook.DEFAULT_WINDOW_SIZE);

        CellStyle style = wb.createCellStyle();
        style.setBorderTop(BorderStyle.THICK);
        style.setBorderBottom(BorderStyle.THICK);

        StylesTable stylesSource = null;
        if (wb instanceof SXSSFWorkbook) {
            stylesSource = ((SXSSFWorkbook) wb).getXSSFWorkbook().getStylesSource();
        } else if (wb instanceof XSSFWorkbook) {
            stylesSource = ((XSSFWorkbook) wb).getStylesSource();
        }
        if (stylesSource != null) {
            ThemesTable theme = stylesSource.getTheme();
            CTXf cellXf = ((XSSFCellStyle) style).getCoreXf();
            setBorderDiagonal(BorderStyle.DOUBLE, stylesSource, cellXf, theme);
        }

        style.setBorderLeft(BorderStyle.THICK);
        style.setBorderRight(BorderStyle.THICK);

        Sheet sheet = wb.createSheet("Sheet1");


            Cell cell = sheet.createRow(1).createCell(2);
            cell.setCellStyle(style);

        FileOutputStream fileOut = new FileOutputStream("CellDiagonalBorders.xlsx");
        wb.write(fileOut);
        wb.close();
        if (wb instanceof SXSSFWorkbook) ((SXSSFWorkbook) wb).dispose();

    }
}